/*
 * Package il.ac.biu.cs.grossmm.test.test2
 * File FooToPidf.java
 * 
 * This is a part of presence service framework API. 
 * See javadoc for more information.
 *  
 * Copyright (C) 2006 Grossmann Mark
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package il.ac.biu.cs.grossmm.tests.test3;

import static il.ac.biu.cs.grossmm.api.data.NodeTypeByInterface.*;
import il.ac.biu.cs.grossmm.api.OperationFailedException;
import il.ac.biu.cs.grossmm.api.data.Root;
import il.ac.biu.cs.grossmm.api.flow.ConverterBase;
import il.ac.biu.cs.grossmm.api.flow.ConverterProxy;
import il.ac.biu.cs.grossmm.api.flow.NodeEventInspector;
import il.ac.biu.cs.grossmm.api.flow.NodeInspector;
import il.ac.biu.cs.grossmm.api.flow.NodeManipulator;
import il.ac.biu.cs.grossmm.api.presence.Pidf;

import java.util.logging.Level;
import java.util.logging.Logger;

public class PidfToFoo extends ConverterBase<Pidf, FooPresence> {

	Logger logger = Logger.getLogger("FooToPidf");

	public PidfToFoo()
	{
		super("Pidf To Presence Converter", nodeType(Pidf.class),
			nodeType(FooPresence.class), 0.5d);
	}

	static class Proxy implements ConverterProxy<Pidf, FooPresence> {
		Logger logger = Logger.getLogger("FooToPidf.Proxy");
		Root<Pidf> pidf;

		Root<FooPresence> fooPresence;

		public Proxy(Root<Pidf> srcNode, Root<FooPresence> dstNode,
			NodeInspector srcInspector, NodeManipulator dstManipulator)
			throws OperationFailedException
		{
			this.fooPresence = dstNode;
			this.pidf = srcNode;
			convert(srcInspector, dstManipulator);
		}

		public void convert(NodeEventInspector srcInspector,
			NodeManipulator dstManipulator) throws OperationFailedException
		{
			convert((NodeInspector) srcInspector, dstManipulator);
		}

		/**
		 * @param srcInspector
		 * @param dstManipulator
		 * @throws OperationFailedException
		 */
		private void convert(NodeInspector srcInspector,
			NodeManipulator dstManipulator) throws OperationFailedException
		{
			String entity = srcInspector.getValue(pidf, Pidf.ENTITY);
			dstManipulator.setValue(fooPresence, FooPresence.ENTITY, entity);
			dstManipulator.setValue(fooPresence, FooPresence.IS_ONLINE, true);
			
			if( logger.isLoggable(Level.FINE))
			logger.fine("Converted "+srcInspector.toString(pidf)+" TO "
				+dstManipulator.toString(fooPresence));
		}

		public void release()
		{
			
		}

	}

	public ConverterProxy<Pidf, FooPresence> createProxy(Root<Pidf> srcNode,
		Root<FooPresence> dstNode, NodeInspector srcInspector,
		NodeManipulator dstManipulator) throws OperationFailedException
	{
		// logger.info("Proxy created");

		return new Proxy(srcNode, dstNode, srcInspector, dstManipulator);
	}
}
